package com.xywl.escort.view.message;

import com.xywl.escort.bean.BaseResp;
import com.xywl.escort.bean.UpdateMsgStatusReq;
import com.xywl.escort.net.BaseSubscriber;
import com.xywl.escort.net.ExceptionHandle;
import com.xywl.escort.net.api.service.ApiServer;
import com.xywl.escort.utils.RxUtil;

import javax.inject.Inject;

/**
 * Created by 32422 on 2018/3/7.
 */

public class MessageDetailPresenter implements MessageDetailContract.Presenter {
    private MessageDetailContract.View mView;
    private ApiServer mApiServer;

    @Inject
    MessageDetailPresenter(ApiServer apiServer) {
        mApiServer = apiServer;
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void unSubscribe() {
        this.mView = null;
    }

    @Override
    public void takeView(MessageDetailContract.View view) {
        this.mView = view;
    }

    @Override
    public void updateMessageStatus(String id, int status) {
        UpdateMsgStatusReq req = new UpdateMsgStatusReq();
        req.id = id;
        req.status = status;
        mApiServer.updateMessageStatus(req)
                .compose(RxUtil.<BaseResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<BaseResp>bindToLife())
                .subscribe(new BaseSubscriber<BaseResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.updateMessageStatusFailure();
                    }

                    @Override
                    public void onNext(BaseResp resp) {
                        if (null != resp && resp.isOk()) {
                            mView.updateMessageStatusSuccess(resp);
                        } else {
                            mView.updateMessageStatusFailure();
                        }
                    }
                });
    }

}
